<!DOCTYPE html>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>角色管理</title>
<script src="js/global1.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
var rmid=null;
var rmrid=null;
var rmmid=null;
var rid=null;
var tt25=null;
var tt26=null;
var tt27=null;
var tt29=null;
	$(function() {
		init();
		
	})
	function mokuai1(){
				
				var rid=$("#rrrid").val();
				$.post(globalData.pre+"/MyModules",{
					rid:rid
				},function(res){
					   tt=res; 
					  for ( i in tt){  
						  
						if (tt[i].mid==25) {
							$("#quanxianadd" ).css("display", "block"); 
						}
						if (tt[i].mid==26) {
							tt26=tt[i].mid; 
						}
						if (tt[i].mid==27) {
							tt27=tt[i].mid; 
						}
						if (tt[i].mid==29) {
							tt29=tt[i].mid; 
						}
				 }
				},"json")
				
				
				
			}
	function init() {
		mokuai1();
		$("#quanxianadd" ).css("display", "none"); 
		$("#tab").datagrid({
			url : globalData.pre+"/selectRoles",
			method : "post",
			pagination : true,
			singleSelect : true,
			toolbar : '#bar',
			queryParams : {
				rid : $("#rid").val(),
				rname : $("#rname").val(),
				rjurisdic : $("#rjurisdic").val(),
			}
		});
		$("#barfrm").form("reset");
	}


	function mokuai(index){
		var data = $("#tab").datagrid("getData");
		var row = data.rows[index]; 
		rid=row.rid;
		rmrid=row.rid;
		rmid=row.rmid;
		jiaoseid = null;
		jiaoseid = row.rid;
		$("#tttree").tree({
			url : globalData.pre+'/selectRolesModulesTree',
			method : 'post',
			checkbox :true,
			 queryParams: {
					rid: rid,
				}
		});
		$('#quanxiantree').window('open');
		
	}
	$(function() {
		$('#quanxiantree').window('close');
	})
	function baocun() {
		var trees = $("#tttree").tree("getChecked", [ "checked", "indeterminate" ]);
		var ids = "";
		for (var i = 0; i < trees.length; i++) {
			if (ids == "") {
				ids = ids + trees[i].id;
			} else {
				ids = ids + "," + trees[i].id;
			}
		}
		$.post(globalData.pre+"/insertRoleModule", {
			rmmid : ids,
			rmrid : jiaoseid
		}, function(res) {
			if (res>0) {
				$('#quanxiantree').window('close');
				$("#tab").datagrid("reload"); 
				$.messager.alert("提示", "分配成功！！！");
			} else {
				$('#quanxiantree').window('close');
				$("#tab").datagrid("reload");
				$.messager.alert("提示", "分配失败！！！");
			}
		}, "json");
		

	}
	
	function deleteRoles(index) {
		var date = $("#tab").datagrid("getData");
		var row = date.rows[index];
		var rid = row.rid;
		$.messager.confirm('确认','您确认想要删除这条记录吗？',function(r){    
		    if (r){   
		    	$.post(globalData.pre+"/deleteRoles", {
					rid : rid
				}, function(res) {
					if (res > 0) {
						alert('删除成功');
						$("#tab").datagrid("reload");
					} else if(res==0){
						alert('删除失败,该角色被占用');
					}else{
						alert('删除失败');
					}
				}, "json")
		    }
		}) 

	}

	function updateguanbi() {
		$("#XiugaiToo").dialog("close");
	}

	function updateU(index) {
		var data = $("#tab").datagrid("getData");
		var row = data.rows[index];
		$("#ff1").form("load", row);
		$("#XiugaiToo").dialog("open");
	}
	function updateBaocun() {
		$.post(globalData.pre+"/updateRoles", {
			rid : $("#rid").val(),
			rname : $("#rname").val(),
			rjurisdic : $("#rjurisdic").val()
		}, function(res) {
			if (res > 0) {
				alert("修改成功");
				$("#XiugaiToo").dialog("close");
				$("#tab").datagrid("reload");
				init();
			} else {
				alert("修改失败");
			}
		}, "json")
	}
	
	
	function openAddwin() {
		$("#addwin").window("open");
	}
	function addok() {
		$.post(globalData.pre+"/insertRoles", {
			rname : $("#rnameadd").val(),
			rjurisdic : $("#rjurisdicadd").val()
		}, function(res) {
			if (res == 1) {
				alert("增加成功");
				$('#addwin').window('close');
				init();
			} else if(res==0){
				alert("角色名已被占用");
				$('#addwin').window('close');
				init();
			}else{
				alert("增加失败");
				$('#addwin').window('close');
				init();
			}
		}, "json")
	}

	function addno() {
		$('#addwin').window('close');
	}
	function formattersanchu(value,row,index){
		if (tt26==26) {
			return "<a href='javascript:void(0)' class='easyui-linkbutton' onclick='deleteRoles(" + index + ")'>删除</a>"			
		}else{
			return "未授权"
		}
	}function formatterxiugai(value,row,index){
		if (tt27==27) {
			return "<a href='javascript:void(0)' class='easyui-linkbutton' onclick='updateU(" + index + ")'>修改</a>"			
		}else{
			return "未授权"
		}
	}function formattermokuai(value,row,index){
		if (tt29==29) {
			return "<a href='javascript:void(0)' class='easyui-linkbutton' onclick='mokuai(" + index + ")'>设置模块</a> "			
		}else{
			return  "未授权"
		}
	}
</script>
</head>
<body>

	<table id="tab" class="easyui-datagrid">
		<thead>
			<tr>
				<th data-options="field:'rid',title:'角色id' "></th>
				<th data-options="field:'rname',title:'角色名称' "></th>
				<th data-options="field:'rjurisdic',title:'角色权限' "></th>
				<th data-options="field:'shezhimokuai',title:'设置模块',formatter:formattermokuai"></th>
				<th data-options="field:'xiugai',title:'修改',formatter:formatterxiugai"></th>
				<th data-options="field:'sanchu',title:'删除',formatter:formattersanchu"></th>
			</tr>
		</thead>
	</table>
  <div id="bar">
		<form id="barfrm" class="easyui-form">
			<label for="name">角色姓名:</label> <input class="easyui-validatebox"
				type="text" id="rname" /> <label for="name">角色权限:</label> <input
				class="easyui-validatebox" type="text" id="rjurisdic" /> <a
				href="javascript:void(0)" class="easyui-linkbutton" onclick="init()"
				data-options="iconCls:'icon-search'">搜索</a>
				<div id="quanxianadd"> </div><a href="javascript:void(0)" class="easyui-linkbutton" 	onclick="openAddwin()" data-options="iconCls:'icon-add'">增加</a>
		</form>
	</div>
	<!--
    	作者：1348946259@qq.com
    	时间：2019-09-20
    	描述：<input  type="hidden"  id="uidn" name="uidn" value="${list.uid }" />
    -->
					<input class="easyui-validatebox" type="hidden" value="1"  id="rrrid"/>
	<div id="XiugaiToo" class="easyui-dialog"
		style="text-align: center; width: 600px; height: 300px"
		data-options="title:'编辑窗口',modal:true,closed:true,
	           	buttons:[{
				text:'保存',
				handler:function(){
				updateBaocun();
				}
			},{
				text:'关闭',
				handler:function(){
				updateguanbi();
				}
			}]">
		<form id="ff1" class="easyui-form">
			<label for="rid">角色id:</label> 
			<input class="easyui-validatebox" type="text" disabled="disabled" id="rid" name="rid" /><br>
			<br> <label for="rname">角色名称:</label>
			 <input class="easyui-validatebox" type="text" id="rname" name="rname" /><br>
			<br> <label for="rjurisdic">角色权限:</label> 
			<input class="easyui-validatebox" type="text" id="rjurisdic" name="rjurisdic" /><br>
			<br>
		</form>

	</div>
	 
<div id="quanxiantree" class="easyui-window" style="width: 300px; height: 600px"
		data-options="iconCls:'icon-save',modal:true">
		<div class="easyui-layout" data-options="fit:true">
			<div data-options="region:'north',split:true" style="height: 535px">
				<div id="mkdiv" class="easyui-panel" style="padding: 5px">
					<ul id="tttree" class="easyui-tree"
						data-options="iconCls:'icon-save',collapsible:true,singleSelect:true"></ul>						
				</div>
				<div data-options="region:'center'" style="text-align: right;">
				<a id="btn" href="javascript:void(0)" style="margin-right: 30px;"
					class="easyui-linkbutton" onclick="baocun()"
					data-options="iconCls:'icon-ok'">保存</a>
			</div>
			</div>
		</div>
	</div>
<div id="addwin" class="easyui-window" title="添加角色"
		style="width: 600px; height: 400px"
		data-options="closed:true,modal:true">
		<form id="addform">
			<table>
				
				<tr>
					<td>角色名称:</td>
					<td><input type="text" class="easyui-textbox" id="rnameadd"
						name="rname" /></td>
				</tr>
				<tr>
					<td>权重:</td>
					<td><input type="text" class="easyui-textbox" id="rjurisdicadd"
						name="rjurisdic" /></td>
				</tr>
				


			</table>
			<a type="button" class="easyui-linkbutton" onclick="addok()">确定添加</a>
			<a type="button" class="easyui-linkbutton" onclick="addno()">取消</a>
		</form>
	</div>

</body>
</html>